<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Canhan extends MY_Controller {
    /**
    * Responsable for auto load the model
    * @return void
    */
   public function __construct(){
		parent::__construct();
		$this->load->model('users_m', 'users');
		$this->load->model('canhan_m', 'canhan');
		$this->load->model('dichvu_m', 'dichvu');		
		$this->load->model('album_m', 'album');		
   }
	
	function index(){
		$data = array();
		//pagination settings
		$config['per_page'] = $this->config->item('pgpb_per_page');
		$config['base_url'] = base_url('pg-pb-model');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $this->config->item('num_links');
		$config['full_tag_open'] = '<ul class="pagination clear">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';
		$config['next_tag_open']	=	'<li>';
		$config['next_tag_close']	=	'</li>';
		$config['next_link']			=	'&rsaquo;';
		$config['prev_link']			=	'&lsaquo;';
		$config['prev_tag_open']	=	'<li>';
		$config['prev_tag_close']	=	'</li>';
		$config['first_tag_open']	=	'<li>';
		$config['first_link']		=	'&lsaquo;&lsaquo;&nbsp;';
		$config['first_tag_close']	=  '</li>';
		$config['last_tag_open']	=	'<li>';
		$config['last_link']			=	'&nbsp;&rsaquo;&rsaquo;';
		$config['last_tag_close']	=	'</li>';
		$config['uri_segment']		= 	2;
		$config['prefix']				=	'';
		$config['suffix']				=	'.html';		
		$config['total_rows'] 		= $this->canhan->get_hoso(array('trang_thai'=>1), true);
		
		$paged = $this->uri->segment(2);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0)
			$paged = 0;
		$tmp = array(
			'limit'		=>	$config['per_page'],
			'paged'		=>	$paged,
			'trang_thai'=> array(1)
		);
		$data['hoso']		= $this->canhan->get_hoso($tmp);
		// echo $this->db->last_query();die;
		if(!count($data['hoso']))
			show_404('404');
		$this->pagination->initialize($config);   
		$data['content'] 	= 'pgpb';
		$data['title'] 	= $paged>0?'Danh sách hồ sơ PG-PB-MODEL - trang '. $paged:'Danh sách hồ sơ PG-PB-MODEL';
		$data['quangcao'] = 'tpl/quangcao';
		$this->load->view('tpl/template', $data);
	}
	
	function view($id = ''){
		if($cn_id = $this->input->post('cn_id')){
			$cn 		=	$this->canhan->get_canhan($cn_id);
			$this->show_phone($cn->di_dong1);
			return;
		}
		$data['canhan']	= $this->canhan->get_canhan($id);
		//echo $this->db->last_query();die;
		if(!$data['canhan'])
			show_404();
		$data['album']			= $this->album->get_album_by_canhan_id($id);
		$data['dichvu']		= $this->dichvu->get_dichvu_dangky_by_canhan_id($id);
		$data['others']		= $this->canhan->get_canhan_khac($id);
		$data['point']			= $this->config->item('point_per_profile');			
		$data['point_download']			= $this->config->item('point_per_download_profile');
		$this->load->model('tuyendung_m', 'tuyendung');			
		$data['is_viewed']		=	$this->tuyendung->is_viewed($data['canhan']->id);
		// print($data['is_viewed']);die;
		// echo '<pre>';print_r($data['dichvu']);die;
		$data['title'] 	= 'Hồ sơ '.$data['canhan']->ho_ten;
		$data['page_title'] 	= 'Hồ sơ '.$data['canhan']->ho_ten;
		$this->load->model('rating_m', 'rating');
		$data['rating']	=	$this->rating->get_rating($id, 'CN');
		$data['rated']	=	false;
		// print_r($this->session->userdata('rated'));die;
		if($rated	=	$this->session->userdata('rated'))			
			if(isset($rated['CN'.$id]))
				$data['rated']	=	true;
		//FB
		$obj = $data['canhan'];
		$data['og_title']	= 'Hồ sơ PG: '.$obj->ho_ten.' - pgvietnamworks.com';
		$desc = 'Mã số: '.$obj->code.' | Ngày sinh: '.date('d/m/Y',strtotime($obj->ngay_sinh)).' | Chiều cao: '.$obj->chieu_cao_m.'m'.$obj->chieu_cao_cm.' | Cân nặng: '.$obj->can_nang.'kg | 3 vòng: '.$obj->vong1.'-'.$obj->vong2.'-'.$obj->vong3 . ' | Kinh nghiệm: '.$obj->kinh_nghiem;		
		$data['og_description']	= $desc;		
		$data['og_image']	= 'http://pgvietnamworks.com/uploads/images/full/'.$obj->avatar;
		//
		$data['content'] 	= 'canhan';
		$data['quangcao'] = 'tpl/quangcao';
		$this->load->view('tpl/template', $data);
	}
	
	function category(){
		$slug	= $this->uri->segment(2);
		if($slug){
			$config = array();			
			$config['per_page'] = $this->config->item('pgpb_per_page');					
			$config['base_url'] = base_url('pg-pb-model/'.$slug);
			$config['use_page_numbers'] = TRUE;
			$config['num_links'] = $this->config->item('num_links');
			$config['full_tag_open'] = '<ul class="pagination clear">';
			$config['full_tag_close'] = '</ul>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a>';
			$config['cur_tag_close'] = '</a></li>';
			$config['next_tag_open']	=	'<li>';
			$config['next_tag_close']	=	'</li>';
			$config['next_link']			=	'&rsaquo;';
			$config['prev_link']			=	'&lsaquo;';
			$config['prev_tag_open']	=	'<li>';
			$config['prev_tag_close']	=	'</li>';
			$config['first_tag_open']	=	'<li>';
			$config['first_link']		=	'&lsaquo;&lsaquo;&nbsp;';
			$config['first_tag_close']	=  '</li>';
			$config['last_tag_open']	=	'<li>';
			$config['last_link']			=	'&nbsp;&rsaquo;&rsaquo;';
			$config['last_tag_close']	=	'</li>';
			$config['uri_segment']		= 	3;
			$config['prefix']				=	'';
			$config['suffix']				=	'.html';
			$config['total_rows'] 		= $this->canhan->get_newest_pg_by_slug(array('slug'		=>	$slug,), true);			
			// echo $this->db->last_query();die;			
			$paged = $this->uri->segment(3);
			$paged = ($paged * $config['per_page']) - $config['per_page'];
			if ($paged < 0)
				$paged = 0;						
			//echo '<pre>';print_r($data['hoso'][0]->ten_dvu);die;
			$data['hoso']	=	$this->canhan->get_newest_pg_by_slug(array(
				'slug'		=>	$slug,
				'limit'		=>	$config['per_page'],
				'paged'		=>	$paged
			));	
			// echo $this->db->last_query();die;			
			if(!count($data['hoso']))
				show_404('404');
			$this->pagination->initialize($config);   
			$data['content'] 	= 'pgpb';
			$data['title'] 	= $paged>0?'Danh sách hồ sơ PG-PB-MODEL - trang '. $paged:'Danh sách hồ sơ PG-PB-MODEL';
			$data['page_title']=	'Hồ sơ '.$data['hoso'][0]->ten_dvu.' mới nhất';
			$data['quangcao'] = 'tpl/quangcao';
			$this->load->view('tpl/template', $data);
		}else
			show_404();
	}
	
	function search(){
		$post = $this->input->post();
		//echo '<pre>';print_r($post);die;
		$fillter = array(
			'chieu_cao_m'=>1,			
			'limit'		=>	12,
			'order_by'	=>	'cn.ngay_cap_nhat',
			'order'		=>	'DESC',
			'paged'		=>	0,
		);
		$config['per_page'] = $this->config->item('pgpb_per_page');
		$config['base_url'] = base_url('pg-pb-model/tim-kiem');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = $this->config->item('num_links');
		$config['full_tag_open'] 	= '<ul class="pagination clear">';
		$config['full_tag_close'] 	= '</ul>';
		$config['num_tag_open'] 	= '<li>';
		$config['num_tag_close'] 	= '</li>';
		$config['cur_tag_open'] 	= '<li class="active"><a>';
		$config['cur_tag_close'] 	= '</a></li>';
		$config['next_tag_open']	=	'<li>';
		$config['next_tag_close']	=	'</li>';
		$config['next_link']			=	'&rsaquo;';
		$config['prev_link']			=	'&lsaquo;';
		$config['prev_tag_open']	=	'<li>';
		$config['prev_tag_close']	=	'</li>';
		$config['first_tag_open']	=	'<li>';
		$config['first_link']		=	'&lsaquo;&lsaquo;&nbsp;';
		$config['first_tag_close']	=  '</li>';
		$config['last_tag_open']	=	'<li>';
		$config['last_link']			=	'&nbsp;&rsaquo;&rsaquo;';
		$config['last_tag_close']	=	'</li>';
		$config['uri_segment']		= 	3;
		$config['prefix']				=	'';
		$config['suffix']				=	'.html';			
		if($post){					
			$fillter['gioi_tinh']	=	$this->input->post('gioi_tinh');
			$fillter['tinh_thanh']	=	$this->input->post('tinh_thanh');
			$fillter['quan_huyen']	=	$this->input->post('quan_huyen');
			if($this->input->post('chieu_cao')){
				$tmp = explode('-',$this->input->post('chieu_cao'));
				$fillter['chieu_cao_cm_min'] = $tmp[0]%100;
				$fillter['chieu_cao_cm_max'] = $tmp[1]%100;
			}
			if($this->input->post('tgian_tu'))
				$fillter['tgian_tu'] 	= $this->input->post('tgian_tu');
			if($this->input->post('tgian_den'))
				$fillter['tgian_den'] 	= $this->input->post('tgian_den');
			if($this->input->post('dichvu'))
				$fillter['dich_vu'] = $this->input->post('dichvu');
			if(trim($this->input->post('key')))
				$fillter['key']	=	$post['key'];
			$this->session->set_userdata('sterm', $fillter);
			//echo '<pre>';print_r($fillter);die;			
		}
		$sterm = $this->session->userdata('sterm');
		// echo '<pre>';print_r($sterm);die;		
		if(!$sterm) $sterm = array();
		$fillter = array_merge($fillter, $sterm);
		$config['total_rows'] 		= $this->canhan->search($fillter, true);
		// echo $this->db->last_query();die;
		$paged = $this->uri->segment(3);
		$paged = ($paged * $config['per_page']) - $config['per_page'];
		if ($paged < 0)
			$paged = 0;						
		$this->pagination->initialize($config);   
		$fillter['limit']	=	$config['per_page'];
		$fillter['paged']	=	$paged;
		$data['hoso']	= $this->canhan->search($fillter, false);
		//echo $this->db->last_query();
		$data['content'] 	= 'pgpb';
		$data['title'] 	= $paged>0?'Kết quả tìm kiếm hồ sơ PG-PB-MODEL - trang '. $paged:'Kết quả tìm kiếm hồ sơ PG-PB-MODEL';
		$data['page_title']=	'Kết quả tìm kiếm hồ sơ PG-PB-MODEL';
		$data['quangcao'] = 'tpl/quangcao';
		$this->load->view('tpl/template', $data);
	}
	
	private function show_phone($di_dong){
		$cn_id		=	$this->input->post('cn_id');		
		$user	=	$this->session->userdata('userdata');		
		if(!$cn_id || !$user)
			echo json_encode(array(
				'status'	=>	'fail',
				'message'=>	'Có lỗi xảy ra. Vui lòng thử lại sau'
			));
		else{
			$dichvu	= 	$this->dichvu->get_dichvu_dangky_by_canhan_id($cn_id);				
			// echo $this->db->last_query();die;
			$this->load->model('tuyendung_m', 'tuyendung');			
			$point	=	$this->config->item('point_per_profile');			
			$point_download	= $this->config->item('point_per_download_profile');
			$obj = $this->canhan->get_canhan($cn_id, true);
			$this->load->model('message_m', 'message');
			$message	=	$this->message->get_message_by_slug('show_profile_content');
			$content = $message->content;
			$href = '<a target="_blank" href="'.site_url('pg-pb-model/'.remove_accents($obj->ho_ten).'-'.$obj->id).'">'. $obj->ho_ten.'</a>';
			$content = str_replace("{profile}", $href, $content);
			$content = str_replace("{point}", $point, $content);
			$row	=	$this->tuyendung->get_phone_catse($user['id'], $cn_id, $point, $content, 1);
			if($row){							
				ob_start();
			?>
				<p><label class="control-label"><strong>Di động:</strong> <span style="color:#ED0304"><?=$di_dong?></span></label></p>
				<table class="table-dashed" width="100%">
					<thead>
						<tr>
							<th width="65%">Dịch vụ</th>
							<th >Giá</th>
						</tr>						
					</thead>
					<tbody>
					<?php foreach($dichvu as $dv):?>
						<tr>
							<td><?=$dv->ten?></td>
							<td style="text-align:right;"><?=$dv->catse ? number_format($dv->catse,0,'','.') : 'Liên hệ'?></td>
						</tr>
					<?php endforeach;?>
					</tbody>
				</table>
				<p>&nbsp;</p>
				<p class="text-center" >
					<a onclick="download_profile(<?=$cn_id?>,<?=$point_download?>,event)" class="btn btn-success" title="Download hồ sơ: <?=$obj->ho_ten?>" >Download hồ sơ</a>
				</p>
			<?php
				$html = ob_get_clean();
			}else{
				ob_start();
			?>
				<h5 style="text-align:justify">Số tiền trong tài khoản không đủ để xem thông tin này. Vui lòng <a href="<?=site_url('nap-tien')?>" title="Nạp thêm tiền" target="_blank"><strong>nạp tiền</strong></a> để xem.</h5>
			<?php
				$html = ob_get_clean();
			}
			echo json_encode(array(
				'status'		=>	'success',
				'html'		=>	$html
			));
		}
		die;
	}
}